package com.zablert.mainactivity.chat

import android.os.Build
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.zablert.mainactivity.BaseRouteConstants.CHAT_ACTIVITY_PATH
import com.zablert.mainactivity.R
import com.zablert.mainactivity.databinding.ActivityChatBinding
import zyc.zalbert.activity.CoreBVMActivity
import zyc.zalbert.extensions.isNotNulOrEmpty


@Route(path = CHAT_ACTIVITY_PATH)
class ChatActivity : CoreBVMActivity<ActivityChatBinding, ChatViewModel>() {
    override fun layoutId(): Int {
        return R.layout.activity_chat
    }

    override fun initViewModel(): ChatViewModel {
        return ChatViewModel()
    }

    private val adapter = ChatAdapter()
    override fun onInitContent() {
        super.onInitContent()
        binding.apply {
            rvMain.adapter = adapter
            rvMain.layoutManager =
                LinearLayoutManager(this@ChatActivity, LinearLayoutManager.VERTICAL, false).apply {
                    //软键盘弹出RecyclerView会随之上移

                }

        }
    }

    override fun onInitViewAction() {
        super.onInitViewAction()
        binding.run {
            zbSend.setOnClickListener {
                zetMessage.text?.toString()?.let {
                    viewModel.sendMessage(it)
                    val diffResult = DiffUtil.calculateDiff(ChatDiffCallback(viewModel.getOld(), viewModel.getMessages()), true)
                    adapter.setItems(viewModel.getMessages())
                    diffResult.dispatchUpdatesTo(adapter)
                    zetMessage.setText("")
                }
            }
            zetMessage.addTextChangedListener(object : TextWatcher {
                override fun afterTextChanged(s: Editable?) {

                }

                override fun beforeTextChanged(
                    s: CharSequence?,
                    start: Int,
                    count: Int,
                    after: Int
                ) {

                }

                override fun onTextChanged(
                    s: CharSequence?,
                    start: Int,
                    before: Int,
                    count: Int
                ) {
                    if (s.isNotNulOrEmpty() && s.toString().trim().isNotEmpty()) {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                            zbSend.setTransitionVisibility(View.VISIBLE)
                        } else {
                            zbSend.visibility = View.VISIBLE
                        }
                    } else {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                            zbSend.setTransitionVisibility(View.GONE)
                        } else {
                            zbSend.visibility = View.GONE
                        }
                    }
                }
            })



        }
    }


}


